'use strict';



angular.module('myApp.networks', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/setting/networks', {
            templateUrl: 'pages/setting/networks/networks.html',
            controller: 'NetworksCtrl'
        });
    }])

    .controller('NetworksCtrl', function ($scope, $http, $location, $rootScope, $filter) {
        $rootScope.show_header = true
        $rootScope.show_footer = true
        $rootScope.title = '以太网'
        console.log($location.path(), ' entered')

        const setEth0 = async (params = {}) => {
            setTimeout(() => {
                updateNetworksInfo()
                swal.close()
            }, 2000)
            if (params.cidr) {
                await $http.post('/system/set_eth0', { cidr: params.cidr })
            } else if (params.netmask) {
                await $http.post('/system/set_eth0', { netmask: params.netmask, ip: params.ip })
            } else if (params.ip) {
                await $http.post('/system/set_eth0', { ip: params.ip })
            } else if (params.gw) {
                await $http.post('/system/set_eth0', { gw: params.gw })
            } else {
            }
        }

        const updateNetworksInfo = () => {
            $http.get('/wifi/ips').then(res => {
                let ips = res.data
                let eth0 = ips.filter(iface => iface.interface == 'eth0')[0]
                console.log(ips, eth0)
                if (eth0) {
                    $scope.eth0 = eth0
                } else {
                    $scope.eth0 = {
                        ip: "",
                        netmask: "",
                        cidr: "",
                        gw: ""
                    }
                }
                $rootScope.items = [
                    { index: 0, value: $scope.eth0.ip },
                    { index: 1, value: $scope.eth0.netmask },
                    { index: 2, value: $scope.eth0.cidr },
                    { index: 3, value: $scope.eth0.gw },
                ]
                $rootScope.rowNum = 4
                $rootScope.colNum = 1
                $rootScope.updatePageInfo()

                // ngRefresh()
            }, (err) => {
                console.log(err)
            })
        }

        const kb = new SoftKeyboard('#soft-keyboard')

        kb.onComplete = (passwd) => {
            if (passwd.length == 0) {
                return
            }
            let i = $rootScope.itemIndex
            let ip = $scope.eth0.ip
            console.log(i, passwd)
            swal.fire({
                title: $filter('translate')("正在设置"),
                text: $filter('translate')("请稍等"),
                // icon: "/images/loading.gif",
                button: false,
                closeOnClickOutside: false,
                closeOnEsc: false
            });
            const ele = document.querySelector('#input')
            ele.classList.add('hide')
            switch (i) {
                case 0:
                    setEth0({ ip: passwd, netmask: $scope.eth0.netmask || '255.255.255.0' })
                    break
                case 1:
                    setEth0({ netmask: passwd, ip: ip || '169.254.50.50' })
                    break
                case 2:
                    setEth0({ cidr: passwd })
                    break
                case 3:
                    setEth0({ gw: passwd })
                    break
            }
        }
        $rootScope.localHandler[$location.path()] = (e) => {
            console.log(e.code)
            if ($rootScope.show.length == 0) {
                return false
            }
            const ele = document.querySelector('#input')
            if (ele && !(ele.classList.contains('hide'))) {
                if (kb.keyHandler(e)) {
                    return true
                }
            }
            switch (e.code) {
                case "Enter":
                    if (ele && ele.classList.contains('hide')) {
                        // $rootScope.itemIndex
                        let value = $rootScope.items[$rootScope.itemIndex].value
                        const title = document.querySelector('#input .title')
                        title.textContent = $filter('translate')('修改')
                        const input = document.querySelector('#passwd')
                        if (value) {
                            input.value = value
                        } else {
                            input.value = ''
                            if ($rootScope.itemIndex == 0) {
                                input.value = '169.254.50.50'
                            }else if ($rootScope.itemIndex == 1) {
                                input.value = '255.255.255.0'
                            }
                            else if ($rootScope.itemIndex == 2) {
                                input.value = '169.254.50.50/16'
                            }
                        }
                        ele.classList.remove('hide')
                    }
                    break;
                case "KeyB":
                    if (ele && !(ele.classList.contains('hide'))) {
                        ele.classList.add('hide')
                    } else {
                        return false
                    }
                    break;
                default:
                    return false
            }
            return true
        }
        updateNetworksInfo()
    });

